package com.wanqing.sms;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(AliyunSMSProperties.class)
@ConditionalOnProperty(name = "wanqing.aliyun.sms.endpoint")
public class AliyunSMSConfiguration {

    @Autowired
    private AliyunSMSProperties properties;

    /**
     * 创建并配置SMS客户端
     * <p>
     * 该方法是一个Spring框架的@Bean方法，用于生成并配置一个Client对象，该对象用于发送短信服务
     * 它从属性文件中读取阿里云的访问密钥ID、访问密钥秘密和终点URL，然后用这些配置创建一个Client实例
     *
     * @return Client 实例化后的短信服务客户端对象
     * @throws RuntimeException 如果在创建Client对象时发生异常，抛出运行时异常
     */
    @Bean
    public Client smsClient() {
        // 初始化配置对象
        Config config = new Config();
        // 设置访问密钥ID
        config.setAccessKeyId(properties.getAccessKeyId());
        // 设置访问密钥秘密
        config.setAccessKeySecret(properties.getAccessKeySecret());
        // 设置服务终点URL
        config.setEndpoint(properties.getEndpoint());
        try {
            // 使用配置对象创建并返回Client对象
            return new Client(config);
        } catch (Exception e) {
            // 如果创建Client对象时发生异常，抛出运行时异常
            throw new RuntimeException(e);
        }

    }

}